from maix import image, camera, display

cam = camera.Camera(320, 240)
disp = display.Display()

A=1
thresholds1 = [[28, 68, 14, 54, -3, 37],[58, 84, -51, -7, 11, 47],[17, 57, -20, 20, -62, -22]] 
thresholds2 = [[28, 68, 14, 54, -10, 37],[52, 92, -51, 4, 9, 47],[33, 72, -20, 12, -62, -13]]   
thresholds3 = [[0, 19, -10, 10, -12, 8],[0,18,-7,13,-21,-1],[8,28,-6,14,-24,-4]]
while 1:
    img = cam.read()
    
    if A == 1:
        objsholds = [thresholds3[0]]
        # img.binary(objsholds)
        # img.dilate(3)
        blobs = img.find_blobs(objsholds, area_threshold=5000,pixels_threshold=10,merge=True)
        for blob in blobs:
            img.draw_rect(blob[0], blob[1], blob[2], blob[3], image.COLOR_GREEN)
            cx = blob[0]+blob[2]/2
            cy = blob[1]+blob[3]/2
            x = blob[0]
            y = blob[1]
            w = blob[2]
            h = blob[3]
        # 计算并打印四个角点
            corners = blob.corners()
            for corner in corners:
                img.draw_circle(corner[0], corner[1], 3, image.Color.from_rgb(255, 0, 0)) # 绘制角点
                print(corners)
            img.draw_cross(int(cx), int(cy), image.Color.from_rgb(255, 0, 0), size=5, thickness=1)
            img.draw_string(blob[0], 10, str(cx)+" "+str(cy), image.Color.from_rgb(255, 0, 0), scale=2)
        disp.show(img)
    

    
